package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Api(tags = "站内信接口")
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private IMessageService messageService;

    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("接收者id") @RequestParam("receiveUserId") @NotNull Long receiveUserId,
                            @ApiParam("内容") @RequestParam("content") @NotNull String content){
        User user=AppConfig.getUser(request);
        if(user.getState()==1){
            log.error(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        if(user.getId().equals(receiveUserId)){
            log.error(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.fail(ResultCode.FAILED_FORBIDDEN);
        }
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);
        return AppResult.success();
    }

    @ApiOperation("获取未读站内信数量")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        User user=AppConfig.getUser(request);
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }

    @ApiOperation("获取个人站内信列表")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        User user=AppConfig.getUser(request);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }

    @ApiOperation("更新已读状态")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                              @ApiParam("站内信id") @RequestParam("id") @NotNull Long id){
        Message message = messageService.selectById(id);
        if(message==null||message.getDeleteState()==1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        User user=AppConfig.getUser(request);
        if(!user.getId().equals(message.getReceiveUserId())){
            log.warn(ResultCode.FAILED_UNAUTHORIZED.toString());
            return AppResult.fail(ResultCode.FAILED_UNAUTHORIZED);
        }
        messageService.updateStateById(id,(byte)1);
        return AppResult.success();
    }

    @ApiOperation("回复帖子")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("被回复帖子id") @RequestParam("repliedId") @NotNull Long repliedId,
                           @ApiParam("回复内容") @RequestParam("content") @NotNull String content){
        User user=AppConfig.getUser(request);
        if(user.getState()==1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        Message existsMessage=messageService.selectById(repliedId);
        if(existsMessage==null||existsMessage.getDeleteState()==1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        if(user.getId().equals(existsMessage.getPostUserId())){
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.fail(ResultCode.FAILED_FORBIDDEN);
        }
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existsMessage.getPostUserId());
        message.setContent(content);
        Date date=new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        messageService.reply(repliedId,message);
        return AppResult.success();
    }
}
